<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <link media="all" rel="stylesheet" th:href="@{/libs/layui-v2.5.5/css/layui.css}">
    <link media="all" rel="stylesheet" th:href="@{/css/public.css}">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input id="menuIds" name="menuIds" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">绑定菜单</label>
        <div class="layui-input-inline">
            <input autocomplete="off" class="layui-input" id="tree" name=""
                   placeholder="请输入" ts-selected="" type="text" value="">
        </div>
        <div class="layui-form-mid layui-word-aux"></div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">角色名</label>
        <div class="layui-input-block">
            <input class="layui-input" lay-reqtext="角色名不能为空" lay-verify="required" name="name" placeholder="请输入角色名"
                   type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">角色代码</label>
        <div class="layui-input-block">
            <input class="layui-input" lay-reqtext="角色代码不能为空" lay-verify="required" name="code" placeholder="请输入角色代码"
                   type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input class="layui-input" lay-reqtext="排序号不能为空" lay-verify="required" name="idx" placeholder="请输入排序号"
                   type="number">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="remark" placeholder="请输入备注信息"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-filter="saveBtn" lay-submit>确认保存</button>
        </div>
    </div>
</div>
</div>
<script charset="utf-8" th:src="@{/libs/layui-v2.5.5/layui.js}"></script>
<script charset="utf-8" th:src="@{/js/lay-config.js}"></script>
<script th:inline="none">
    layui.use(['form', 'laydate', 'tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#tree',
            checkedKey: 'id',
            height: '400',
            width: '900',
            searchType: 'more',
            searchList: [
                {searchKey: 'title', searchPlaceholder: '菜单名'},
            ],
            table: {
                response: {
                    statusCode: 200
                },
                url: '/system/menu/listPage',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID'},
                    {field: 'title', title: '菜单名'},
                    {field: 'url', title: '菜单url'},
                    {
                        field: 'icon', title: '图标', align: 'center', templet: function (d) {
                            return '<i class="fa ' + d.icon + '" style="width: 30px"></i>'
                        }
                    },
                    {field: 'idx', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                            // if (d.isMenu == 1) {
                            //     return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            // }
                            if (d.parentId === -1) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '类型'
                    },
                ]]
            },
            done: function (elem, data) {
                var NEWJSON = []
                var IDS = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.title)
                    IDS.push(item.id)
                })
                elem.val(NEWJSON.join(","))
                $("#menuIds").val(IDS.toString())
            }
        })

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("是否确认添加", {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    type: "PUT",
                    url: "/system/role/add",
                    data: {
                        "name": data.field.name,
                        "code": data.field.code,
                        "idx": data.field.idx,
                        "menuIds": data.field.menuIds,
                        "remark": data.field.remark
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.code === 200) {
                            // 关闭弹出层
                            layer.alert(data.message)
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        } else {
                            layer.alert(data.message)
                        }
                    }
                });
            });
            return false;
        });
    });
</script>
</body>
</html>